package com.zzyl.serve.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.service.INursingProjectService;
import com.zzyl.serve.vo.NursingProjectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
/**
 * 护理项目Service业务层处理
 * 
 * @author zzyl
 * @date 2025-10-13
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return this.getById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        boolean flag = this.save(nursingProject);
        deleteCache();
        return flag ? 1 : 0;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        boolean flag = this.updateById(nursingProject);
        deleteCache();
        return flag ? 1 : 0;
    }

/*    *//**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     *//*
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        return this.removeBatchByIds(Arrays.asList(ids)) == true ? 1 : 0;
    }*/

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        boolean flag = this.removeById(id);
        deleteCache();
        return flag ? 1 : 0;
    }

    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVO> getProjectAll() {

        List<NursingProjectVO> projectVOList = (List<NursingProjectVO>) redisTemplate.opsForValue().get(CacheConstants.NURSINGPROJECT_ALL);
        if (CollUtil.isNotEmpty(projectVOList)) {
            return projectVOList;
        }

        // 查询数据
        projectVOList = nursingProjectMapper.getProjectAll();
        // 缓存数据到redis中
        redisTemplate.opsForValue().set(CacheConstants.NURSINGPROJECT_ALL, projectVOList);

        return projectVOList;
    }

    /**
     * 删除redis中的旧缓存
     */
    private void deleteCache() {
        redisTemplate.delete(CacheConstants.NURSINGPROJECT_ALL);
    }
}
